using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.Experimental;
using UnityEngine;

namespace Framework
{
    public class GameObjectFieldsSetting : ScriptableObject
    {

        [System.Serializable]
        public class GameObjectTypeIcon
        {
            public string Name;
            public Texture2D Icon;

        }
        public bool Hint = true;
        public Texture2D MainIcon;
        public Texture2D DefaultNodeIcon;
        public bool ShowButton = true;
        public bool ShowLabel = true;
        public Color32 LabelColor;
        public List<GameObjectTypeIcon> TypeIcons = new List<GameObjectTypeIcon>();
        private const string ConfigFile = FrameworkSetting.SETTING_FOLDER + "GameObjectFieldsSetting.asset";
        private static GameObjectFieldsSetting instance = null;
        public static GameObjectFieldsSetting Instance
        {
            get
            {
                if (instance == null)
                {
                    if (File.Exists(ConfigFile))
                    {
                        instance = AssetDatabase.LoadAssetAtPath<GameObjectFieldsSetting>(ConfigFile);
                    }
                    else
                    {
                        FrameworkSetting.CreateFolder();
                        instance = CreateInstance<GameObjectFieldsSetting>();
                        AssetDatabase.CreateAsset(instance, ConfigFile);

                    }
                    if (instance != null)
                    {
                        instance.LabelColor = new Color32(0, 0, 0, 255);
                    }
                }
                if (instance == null)
                {
                    return null;
                }
                if (instance.MainIcon == null || instance.DefaultNodeIcon == null)
                {
                    var p = Utils.File.GetCurrentScriptFilePath();
                    p = Path.GetDirectoryName(Utils.File.GetAssetsPathByAbsPath(p));
                    var list = new string[] {
                        "GameObjectMain",
                        "GameObjectFieldsItem",
                    };
                    var Icons = new List<Texture2D>(list.Length);
                    var len = list.Length;
                    for (var i = 0; i < len; i++)
                    {
                        Icons.Add(null);
                        var iconPath = Path.Combine(p, "EditorResources", list[i] + ".png");
                        if (File.Exists(iconPath))
                        {
                            var icon = EditorResources.Load<Texture2D>(iconPath);
                            Icons[i] = icon;
                        }
                    }

                    instance.MainIcon = Icons[0];
                    instance.DefaultNodeIcon = Icons[1];
                }
                return instance;
            }
        }
        public Texture2D GetNodeIcon(string name = null)
        {
            return DefaultNodeIcon;
        }
        public Texture2D GetMainIcon()
        {
            return MainIcon;
        }
        public GameObjectTypeIcon GetIcon(string typeName)
        {
            foreach (var item in this.TypeIcons)
            {
                if (item.Name == typeName)
                {
                    return item;
                }
            }
            return null;
        }
    }
}